import {type ElForm, type ElTable, type ElUpload, FormItemRule} from 'element-plus';
import { getToken } from '@/utils/auth';
import { getCurrentInstance, ref } from 'vue';
const baseURL = import.meta.env.VITE_APP_BASE_API;
export default (getList:any)=>{
	const { proxy } = getCurrentInstance() as any;
	// 用户导入参数
	const upload = ref<any>({
		// 是否显示弹出层（用户导入）
		open: false,
		// 弹出层标题（用户导入）
		title: '',
		// 是否禁用上传
		isUploading: false,
		// 是否更新已经存在的用户数据
		updateSupport: 0,
		// 设置上传的请求头部
		headers: {Authorization: 'Bearer ' + getToken()},
		// 上传的地址
		url: baseURL + '/manage/content/importData',
		loading:false
	});
	const uploadRef = ref<InstanceType<typeof ElUpload>>();
	
	/** 导入按钮操作 */
	const handleImport = () => {
		upload.value.title = '用户导入';
		upload.value.open = true;
	};

	/**
     * 清除上传控件选中文件
     */
	const cleanUploadRef = () => {
		uploadRef.value?.clearFiles();
		upload.value.updateSupport = 0;
	};

	/** 下载模板操作 */
	const importTemplate = () => {
		proxy.download(
			'/manage/content/importTemplate',
			{},
			`content_template_${new Date().getTime()}.xlsx`,
		);
	};

	// 文件上传中处理
	const handleFileUploadProgress = (event: any, file: any, fileList: any) => {
		upload.value.isUploading = true;
		// upload.value.loading = true;
	};

	// 文件上传成功处理
	const handleFileSuccess = (response: any, file: any, fileList: any) => {
		upload.value.open = false;
		upload.value.isUploading = false;
		upload.value.loading = false;
		// Proxy.$refs.upload.clearFiles();
		cleanUploadRef();
		proxy.$alert(response.msg, '导入结果', {
			dangerouslyUseHTMLString: true,
		});
		getList();
	};

	// 提交上传文件
	const submitFileForm = () => {
		proxy.$refs.uploadRef.submit();
	};
	return {
		handleImport,
		cleanUploadRef,
		importTemplate,
		handleFileSuccess,
		handleFileUploadProgress,
		submitFileForm,
		upload,
		uploadRef
	};
};